﻿using System;
using System.Windows.Forms;

namespace hello_accinv_New_
{
    public partial class SaleReportForm : Form
    {
        private Database db;
        public string loginName;

        public SaleReportForm()
        {
            InitializeComponent();

            db = new Database();

            radioToday.Text = "Today (" + DateTime.Today.ToLongDateString() + ")";

            dtFrom.MaxDate = DateTime.Today;
            dtTo.MaxDate = DateTime.Today;
        }

        private void radioToday_CheckedChanged(object sender, EventArgs e)
        {
            if (radioToday.Checked)
            {
                dtFrom.Enabled = false;
                dtTo.Enabled = false;

                LoadReport(DateTime.Today);
            }
        }

        private void radioFromTo_CheckedChanged(object sender, EventArgs e)
        {
            if (radioFromTo.Checked)
            {
                dtFrom.Enabled = true;
                dtTo.Enabled = true;

                LoadReport(dtFrom.Value, dtTo.Value);
            }
        }

        private void dtFrom_ValueChanged(object sender, EventArgs e)
        {
            LoadReport(dtFrom.Value, dtTo.Value);
        }

        private void dtTo_ValueChanged(object sender, EventArgs e)
        {
            LoadReport(dtFrom.Value, dtTo.Value);
        }

        private void LoadReport(DateTime selectedDate)
        {
            dataGridView.DataSource = db.Select("S.Date, S.Time, S.SaleID, P.PCode, P.Name, PS.Quantity, PS.UnitPrice, PS.Discount, PS.TotalPrice, PS.Remark","Sale S INNER JOIN ProductSale PS ON S.SaleID = PS.SaleID INNER JOIN Product P ON PS.ProductCode = P.Pcode","S.Date = '"+CustomDateTime.ToSqlDate(selectedDate)+"'");
            dataGridView.ClearSelection();
        }

        private void LoadReport(DateTime startDate, DateTime endDate)
        {
            dataGridView.DataSource = db.Select("S.Date, S.Time, S.SaleID, P.PCode, P.Name, PS.Quantity, PS.UnitPrice, PS.Discount, PS.TotalPrice, PS.Remark", "Sale S INNER JOIN ProductSale PS ON S.SaleID = PS.SaleID INNER JOIN Product P ON PS.ProductCode = P.Pcode", "S.Date BETWEEN '" + CustomDateTime.ToSqlDate(startDate) + "' AND '" + CustomDateTime.ToSqlDate(endDate) + "'");
            dataGridView.ClearSelection();
        }

        private void dataGridView_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            if (dataGridView.RowCount > 0)
            {
                ReRunNumber();
                ReCalculate();
            }
            else
            {
                txtboxTotalDiscount.Clear();
                txtboxTotalPrice.Clear();
            }
        }

        private void ReRunNumber()
        {
            for (int i = 1; i <= dataGridView.RowCount; i++)
            {
                dataGridView[0, (i - 1)].Value = i;
            }
        }

        private void ReCalculate()
        {
            decimal totalD, totalP; totalD = totalP = 0M;

            for (int i = 0; i < dataGridView.RowCount; i++)
            {
                totalD += Decimal.Parse(dataGridView[8, i].Value.ToString());
                totalP += Decimal.Parse(dataGridView[9, i].Value.ToString());
            }

            txtboxTotalDiscount.Text = totalD.ToString("#,#0.00#");
            txtboxTotalPrice.Text = totalP.ToString("#,#0.00#");
        }
    }
}
